package initialize

import (
	"errors"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"rpc/global"
	"rpc/model"
	"time"
)

func InitMysql() {
	var err error
	res := global.AppData.Service.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		res.Username, res.Password, res.Host, res.Port, res.Dbname)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})

	if err != nil {
		panic(errors.New("数据库链接失败"))
	}
	fmt.Println("数据库链接成功")
	//链接池
	sqlDB, err := global.DB.DB()
	sqlDB.SetMaxIdleConns(10)
	sqlDB.SetMaxOpenConns(100)
	sqlDB.SetConnMaxLifetime(time.Hour)

	global.DB.AutoMigrate(&model.VideoReleaseWeek3{})

}
